//
//  VideoListVC.swift
//  UlTableView-Programmatic
//
//  Created by jack on 2025/10/27.
//

import UIKit

class VideoListVC: UIViewController {
    
    var constcellid  = "videoVell"
    var tableView  = UITableView()
    var videos: [Video] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        videos = fetchData()
        configureTableView();

        // Do any additional setup after loading the view.
    }
    
    func configureTableView() {
        view.addSubview(tableView)
        //设置代理
        setTableViewDelegates();
        
        //设置行高
        tableView.rowHeight = 100
        
        // 注册单元格
        tableView.register(VideoCell.self, forCellReuseIdentifier: constcellid)
        //设置约束
        tableView.pin(to: view)
    
       
    }
    
    //设置委托代理
    func setTableViewDelegates() {
        tableView.delegate = self
        tableView.dataSource = self
    }
    
}

extension VideoListVC:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return videos.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: constcellid) as! VideoCell
        let video = videos[indexPath.row]
        cell.set(video: video)
       return cell
    }
    
    
}

extension VideoListVC {
    func fetchData() -> [Video] {
        let video1 = Video(image: Images.ninety, title: "No Storyboards")
        let video2 = Video(image: Images.softSkills, title: "5 Soft Skills For Developers")
        let video3 = Video(image: Images.xcode, title: "What's New in Xcode 11")
        let video4 = Video(image: Images.patreon, title: "Patreon Revamp")
        let video5 = Video(image: Images.raise, title: "How I Got a Raise. $60k - $100k")
        let video6 = Video(image: Images.shake, title: "Shake Gesture")
        let video7 = Video(image: Images.salaries, title: "2019 State of Salaries")
        let video8 = Video(image: Images.wireless, title: "How to Build Your App Wirelessly")
        let video9 = Video(image: Images.swiftNews, title: "Swift News 70")
        let video10 = Video(image: Images.ninety, title: "The 90/90 Rule")
        
        return [video1, video2, video3, video4, video5, video6, video7, video8, video9, video10]
    }
}
